package com.woniuxy.controller;

import com.woniuxy.entity.RbacManager;
import com.woniuxy.service.RbacManagerService;
import com.woniuxy.utils.Chinese;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/rbacManager")
public class RbacManagerController {
    @Autowired
    RbacManagerService rbacManagerService;
    @Autowired
    Chinese chinese;
    /**
     * 查询所有用户
     * ChangeLog : 1. 创建 (22/08/24/0024 14:23 [马宇航]);
     *  @RequestMapping("/findAll") 相当于，url请求：http://localhost:8080/rbacManager/findAll
     * @return java.lang.String
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public String findAll(){
        List<RbacManager> managers = rbacManagerService.findAll();
        return chinese.toString();
    }
    //The target resource does not have a
    // current representation that would be acceptable to the user agent,
    // according to the proactive negotiation header fields received in the request,
    // and the server is unwilling to supply a default representation.
    @RequestMapping("/findByName") //http://localhost:8080/rbacManager/findByName？name=sfs
    @ResponseBody
    public List findByName(@RequestParam("name") String username){
        List<RbacManager> list = rbacManagerService.findByName(username);
        return list;
    }
    //限制请求为POST请求 @PostMapping 也行！
    @RequestMapping(value = "/findByIds",method = RequestMethod.POST)
    public String findByIds(Integer[] ids, Model model){
        List<RbacManager> list = rbacManagerService.findByIds(ids);
        model.addAttribute("data",list);
        return "success";
    }
    @RequestMapping(value = "/findByLists",method = RequestMethod.POST)
    public String findByLists(@RequestParam List<Integer> ids, Model model){
        Integer[] objects = ids.toArray(new Integer[0]);
        List<RbacManager> list = rbacManagerService.findByIds(objects);
        model.addAttribute("data",list);
        return "success";
    }
    @RequestMapping(value = "/addOneForMap")
    public String addOneForMap(@RequestParam Map<String,Object> userinfo,Model model){
        if (userinfo.size()!=0) {
            int i = rbacManagerService.addOneForMap(userinfo);
        }
        model.addAttribute("data","新增用户："+userinfo);
        return "success";
    }
    @RequestMapping("/addOne")
    public String addOne(RbacManager rbacManager,Model model){
        int i = rbacManagerService.addOne(rbacManager);
        model.addAttribute("data","新增用户："+rbacManager);
        return "success";
    }
    @PostMapping(value = "/addOneJson",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public RbacManager addOneJson(@RequestBody RbacManager rbacManager){
        int i = rbacManagerService.addOne(rbacManager);
        return rbacManager;
    }
}